Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
Warning Each child in a list should have a unique "key" prop

I have an array of objects which I use to list values in page with map. But from time to time I receive this error.

Warning: Each child in a list should have a unique "key" prop.

Although keys are unique.

Maybe anyone knows what could be wrong here?

const items = [
  {key: 1, name: 'Item one', value: 34 },
  {key: 2, name: 'Item two', value: 45 },
  {key: 3, name: 'Item three', value: 12 },
]

const item = ({ name, value, key }) => (
    <div>
      <p>{name}</p>
      <p>{value}</p>
    </div>
  )

return(
 <div>
   {items.map(i => item(i))}
 </div>
)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

item needs to be a component, and React component names need to be capitalised. Your Item component is expecting an object. Your "key" needs to be placed on the mapped component.

// Accepts items
// From each object in the array it gets the
// key, name, and value, and returns a new
// component
function Example({ items }) {
  return (
    <div>
      {items.map(item => {
        const { key, value, name } = item;
        return <Item key={key} value={value} name={name} />
      })}
    </div>
   );
}

// Accepts an object - returns some JSX to be rendered
function Item({ name, value }) {
  return (
    <div>
      <p>{name}</p>
      <p>{value}</p>
    </div>
  );
}

const items = [
  {key: 1, name: 'Item one', value: 34 },
  {key: 2, name: 'Item two', value: 45 },
  {key: 3, name: 'Item three', value: 12 },
];

ReactDOM.render(
  <Example items={items} />,
  document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

you just need to pass the key in child div as I have done in your code below. I think it will remove your error below

import React from 'react';
import './style.css';

export default function App() {
  const items = [
    { key: 1, name: 'Item one', value: 34 },
    { key: 2, name: 'Item two', value: 45 },
    { key: 3, name: 'Item three', value: 12 },
  ];
  const item = ({ name, value, key }) => (
    <div key={key}>
      <p>{name}</p>
      <p>{value}</p>
    </div>
  );
  return <div>{items.map((i) => item(i))}</div>;
}

For refer:https://reactjs.org/docs/lists-and-keys.html

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda